# -*- coding: UTF-8 -*-


class BaseModel(object):
    _model = None
    _emv = None

    def __init__(self, id):
        self.id = id

    @classmethod
    def call(cls, method, *args):
        return cls._env.rpc.call(cls._model, method, *args)

    @classmethod
    def call_kw(cls, method, *args, **kwargs):
        return cls._env.rpc.call_kw(cls._model, method, args, kwargs)

    @classmethod
    def search(cls, domain, offset=None, limit=None, order=None):
        method = 'search'
        return cls.call_kw(method, domain, offset=offset, limit=limit, order=order)

    @classmethod
    def search_read(cls, domain=None, fields=None, offset=0, limit=None, order=None):
        method = 'search_read'
        return cls.call_kw(method, domain=domain, fields=fields, offset=offset, limit=limit, order=order)

    @classmethod
    def read(cls, ids, fields=None):
        method = 'read'
        return cls.call_kw(method, ids, fields=fields)

    def read(self,  fields=None):
        return self.__class__.read(self.id, fields)

    @classmethod
    def browse(cls, ids):
        return cls(ids)

    @classmethod
    def create(cls, vals, context=None):
        method = 'create'
        return cls.call_kw(method, vals, context=context)

    @classmethod
    def write(cls, res_id, vals):
        method = 'write'
        return cls.call(method, res_id, vals)

    @classmethod
    def name_get(cls, res_ids):
        method = 'name_get'
        return cls.call(method, res_ids)

    @classmethod
    def name_search(cls, domain):
        method = 'name_search'
        return cls.call(method, domain)

    @classmethod
    def unlink(cls, ids):
        method = 'unlink'
        return cls.call(method, ids)

    @classmethod
    def fields_get(cls, allfields=None, attributes=None):
        return cls.call('fields_get', allfields, attributes)

    @classmethod
    def onchange(cls, *args, **kwargs):
        return cls.call_kw('onchange', *args, **kwargs)
